#include "stdafx.h"
#include "CActiveStatu.h"
#include "CGraphicMgr.h"
#include "XMLParser.h"
#include "iolib.h"
#include "Aes.h"
CActiveStatu::CActiveStatu(WCHAR * name):IStatu(name)
{
	memset(m_szCode,0,256);
}

CActiveStatu::~CActiveStatu(void)
{
}
bool CActiveStatu::EnterStatu()
{

	m_ActiveNum=(CStaticText*)GRAPHICMGR.GetElement(L"txtActiveNum");

	m_ActiveNum->setvalue(m_szCode);
	return true;
}
bool CActiveStatu::backClicked()
{

   return true;
}
bool CActiveStatu::OkClicked()
{

	return true;
}
bool CActiveStatu::ActiveSoftware()
{
	CXMLParser xmlParser;
	int bflag=0;
	wstring strKey,strCode;
	BYTE bUUID[128] = {0};
    xmlParser.Load(REG_BINFSXML);

    xmlParser.ResetMainPos();
    xmlParser.FindChildElem(L"Configer");

   
	strKey = xmlParser.GetChildAttrib(L"Num");
	char stTime[128]={0};
	unsigned char  szCode[128] = { 0 };
	WChar2MByte(strKey.c_str(),stTime,128);
	GetUUID(bUUID);
	Aes *pAesEncode = new Aes(Bits128, (unsigned char *)stTime); 
	pAesEncode->Cipher((BYTE*)bUUID,szCode);
	delete pAesEncode;

	char serial[512]={0};
	{	
		char tmpBuffer[10]={0};
		for(unsigned i=0;i <16 ;i++)
		{
			sprintf(tmpBuffer, ("%02X"), szCode[i]);
			strcat(serial, tmpBuffer);
		}
	}



     memset(szCode,0,32);
	 unsigned char buff2[32]={0};
	 memcpy(buff2,(BYTE*)&serial[22],10);

    pAesEncode = new Aes(6,(unsigned char*)"\x0\x1\x2\x3\x4\x5\x6"); 

	pAesEncode->Cipher(buff2,szCode);
	delete pAesEncode;


    memset(serial,0,512);
	{	
		char tmpBuffer[10]={0};
		for(unsigned i=0;i <16 ;i++)
		{
			sprintf(tmpBuffer, ("%02X"), szCode[i]);
			strcat(serial, tmpBuffer);
		}
	}

     memset(szCode,0,128);
	WChar2MByte(m_szCode,(LPSTR)szCode,128);
	if (0==memcmp(serial,szCode,10))
	{
	    TCHAR szTemp[256]={0};
		MByte2WChar(serial, szTemp, 256);
		wstring  strActiveNum=szTemp;
		
	  strActiveNum=strActiveNum.substr(0,10);
		xmlParser.SetChildAttrib(L"Run",0);

		
		
		xmlParser.SetChildAttrib(L"Code",strActiveNum.c_str());
		xmlParser.Save(REG_BINFSXML);
		return true;
	}
	else
	{
		return false;
	}

	/*TCHAR szTemp[256]={0};
	MByte2WChar(serial, szTemp, 256);
	wstring  strActiveNum=&szTemp[22];
	CStaticText *pActiveNum=(CStaticText*)GRAPHICMGR.GetElement(L"txtDeviceActiveNum");
	pActiveNum->setvalue(strActiveNum.c_str());*/
	return true;
}
bool  CActiveStatu::EvaluateClickEvent(const WCHAR* elementName)
{
	 if (wcscmp(elementName,L"active_btn_Back") == 0)  
	{
			int iStrLength = _tcslen(m_szCode);
			m_szCode[iStrLength-1] = L'\0'; 
			m_ActiveNum->setvalue(m_szCode);
			GRAPHICMGR.SetUpdateElements(L"txtActiveNum",L"none");

       return true;
	}
	 else if (wcscmp(elementName,L"active_btn_Active") == 0)  
	 {


		
       if (ActiveSoftware())
       {
		  GRAPHICMGR.PostChange(L"mainhome");
       }



      return true;

	 }
	 else if (wcscmp(elementName,L"active_btn_Exit") == 0)  
	 {
		 /* int iStrLength = _tcslen(m_szCode);
		 m_szCode[iStrLength-1] = L'\0'; 
		 m_ActiveNum->setvalue(m_szCode);
		 GRAPHICMGR.SetUpdateElements(L"txtActiveNum",L"none");*/
		PostMessage(GRAPHICMGR.GetWnd(), WM_CLOSE, 0, 0);
		 return true;

	 }
	else if(wcscmp(elementName,L"active_btn_1") == 0)
	{
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 40)
		{
			goto exit;
		}
		m_szCode[iStrLength] = L'1';
		m_szCode[iStrLength + 1] = L'\0'; 
		m_ActiveNum->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"txtActiveNum",L"none");

		//SendSingleCode((char)m_szCode);
		return true;
	}
	else if(wcscmp(elementName,L"active_btn_2") == 0)
	{
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 40)
		{
			goto exit;
		}
		m_szCode[iStrLength] = L'2';
		m_szCode[iStrLength + 1] = L'\0'; 
		m_ActiveNum->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"txtActiveNum",L"none");

		//SendSingleCode((char)m_szCode);
		return true;
	}
	else if(wcscmp(elementName,L"active_btn_3") == 0)
	{
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 40)
		{
			goto exit;
		}
		m_szCode[iStrLength] = L'3';
		m_szCode[iStrLength + 1] = L'\0'; 
		m_ActiveNum->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"txtActiveNum",L"none");

		//SendSingleCode((char)m_szCode);
		return true;
	}
	else if(wcscmp(elementName,L"active_btn_4") == 0)
	{
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 40)
		{
			goto exit;
		}
		m_szCode[iStrLength] = L'4';
		m_szCode[iStrLength + 1] = L'\0'; 
		m_ActiveNum->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"txtActiveNum",L"none");

		//SendSingleCode((char)m_szCode);
		return true;
	}
	else if(wcscmp(elementName,L"active_btn_5") == 0)
	{
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 40)
		{
			goto exit;
		}
		m_szCode[iStrLength] = L'5';
		m_szCode[iStrLength + 1] = L'\0'; 
		m_ActiveNum->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"txtActiveNum",L"none");

		//SendSingleCode((char)m_szCode);
		return true;
	}
	else if(wcscmp(elementName,L"active_btn_6") == 0)
	{
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 40)
		{
			goto exit;
		}
		m_szCode[iStrLength] = L'6';
		m_szCode[iStrLength + 1] = L'\0'; 
		m_ActiveNum->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"txtActiveNum",L"none");

		//SendSingleCode((char)m_szCode);
		return true;
	}
	else if(wcscmp(elementName,L"active_btn_7") == 0)
	{
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 40)
		{
			goto exit;
		}
		m_szCode[iStrLength] = L'7';
		m_szCode[iStrLength + 1] = L'\0'; 
		m_ActiveNum->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"txtActiveNum",L"none");

		//SendSingleCode((char)m_szCode);
		return true;
	}
	else if(wcscmp(elementName,L"active_btn_8") == 0)
	{
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 40)
		{
			goto exit;
		}
		m_szCode[iStrLength] = L'8';
		m_szCode[iStrLength + 1] = L'\0'; 
		m_ActiveNum->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"txtActiveNum",L"none");

		//SendSingleCode((char)m_szCode);
		return true;
	}
	else if(wcscmp(elementName,L"active_btn_9") == 0)
	{
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 40)
		{
			goto exit;
		}
		m_szCode[iStrLength] = L'9';
		m_szCode[iStrLength + 1] = L'\0'; 
		m_ActiveNum->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"txtActiveNum",L"none");

		//SendSingleCode((char)m_szCode);
		return true;
	}
	else if(wcscmp(elementName,L"active_btn_0") == 0)
	{
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 40)
		{
			goto exit;
		}
		m_szCode[iStrLength] = L'0';
		m_szCode[iStrLength + 1] = L'\0'; 
		m_ActiveNum->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"txtActiveNum",L"none");

		//SendSingleCode((char)m_szCode);
		return true;
	}
	else if(wcscmp(elementName,L"active_btn_A") == 0)
	{
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 40)
		{
			goto exit;
		}
		m_szCode[iStrLength] = L'A';
		m_szCode[iStrLength + 1] = L'\0'; 
		m_ActiveNum->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"txtActiveNum",L"none");

		//SendSingleCode((char)m_szCode);
		return true;
	}
	else if(wcscmp(elementName,L"active_btn_B") == 0)
	{
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 40)
		{
			goto exit;
		}
		m_szCode[iStrLength] = L'B';
		m_szCode[iStrLength + 1] = L'\0'; 
		m_ActiveNum->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"txtActiveNum",L"none");

		//SendSingleCode((char)m_szCode);
		return true;
	}
else if(wcscmp(elementName,L"active_btn_C") == 0)
	{
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 40)
		{
			goto exit;
		}
		m_szCode[iStrLength] = L'C';
		m_szCode[iStrLength + 1] = L'\0'; 
		m_ActiveNum->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"txtActiveNum",L"none");

		//SendSingleCode((char)m_szCode);
		return true;
	}
else if(wcscmp(elementName,L"active_btn_D") == 0)
{
	int iStrLength = _tcslen(m_szCode);
	if ( iStrLength >= 40)
	{
		goto exit;
	}
	m_szCode[iStrLength] = L'D';
	m_szCode[iStrLength + 1] = L'\0'; 
	m_ActiveNum->setvalue(m_szCode);
	GRAPHICMGR.SetUpdateElements(L"txtActiveNum",L"none");

	//SendSingleCode((char)m_szCode);
	return true;
}
else if(wcscmp(elementName,L"active_btn_E") == 0)
{
	int iStrLength = _tcslen(m_szCode);
	if ( iStrLength >= 40)
	{
		goto exit;
	}
	m_szCode[iStrLength] = L'E';
	m_szCode[iStrLength + 1] = L'\0'; 
	m_ActiveNum->setvalue(m_szCode);
	GRAPHICMGR.SetUpdateElements(L"txtActiveNum",L"none");

	//SendSingleCode((char)m_szCode);
	return true;
}
else if(wcscmp(elementName,L"active_btn_F") == 0)
{
	int iStrLength = _tcslen(m_szCode);
	if ( iStrLength >= 40)
	{
		goto exit;
	}
	m_szCode[iStrLength] = L'F';
	m_szCode[iStrLength + 1] = L'\0'; 
	m_ActiveNum->setvalue(m_szCode);
	GRAPHICMGR.SetUpdateElements(L"txtActiveNum",L"none");

	//SendSingleCode((char)m_szCode);
	return true;
}
exit:

	return false;

}